package report.design.mainframe;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JToolBar;

import report.design.config.MenuBarDef;
import report.design.config.ToolBarDef;
import report.utils.GUIUtils;

public class DesignerFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JDesktopPane jdesktopPane;

	public DesignerFrame(){
		DesignerContext.setDesignerFrame(this);
		this.setJMenuBar(new MenuBarDef());
		
		JToolBar toolBar = ToolBarDef.createJToolBar();
		this.add(toolBar, BorderLayout.NORTH);
		
		jdesktopPane = new JDesktopPane();
		jdesktopPane.setLayout(new FlowLayout());
		this.add(jdesktopPane, BorderLayout.CENTER);
		
		GUIUtils.fullScreenWindow(this);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void newReport(){
		JReportInternalFrame internalFrame = new JReportInternalFrame();
		jdesktopPane.add(internalFrame);
		jdesktopPane.setSelectedFrame(internalFrame);
	}
	
	public void saveReport(){
		JReportInternalFrame jif = (JReportInternalFrame) jdesktopPane.getSelectedFrame();
		if(jif != null){
			jif.save();
		}
	}
	
	public void openReport(){
		JReportInternalFrame jif = new JReportInternalFrame();
		jif.open();
		jdesktopPane.add(jif);
		jdesktopPane.setSelectedFrame(jif);
	}
	
	public void closeReport(){
		JInternalFrame jif = jdesktopPane.getSelectedFrame();
		if(jif != null){
			jif.dispose();
		}
		JInternalFrame[] jifs = jdesktopPane.getAllFrames();
		if(jifs.length > 0){
			jdesktopPane.setSelectedFrame(jifs[0]);
		}
	}
	
	public static void main(String[] args) {
		new DesignerFrame();
	}
}
